package all

//主体

type Subject interface {
	Register(Observer)
	DeRegister(Observer)
	NotifyAll()
}

// Iterm 主体的具体实现
type Iterm struct {
	Observers []Observer
	Name      string
	IsStock   bool
}

func (i *Iterm) Register(o Observer) {
	i.Observers = append(i.Observers, o)
}

func (i *Iterm) DeRegister(o Observer) {
	i.Observers = i.Remove(i.Observers, o)
}

func (i *Iterm) NotifyAll() {
	for _, o := range i.Observers {
		o.Update(i.Name)
	}
}

func (i *Iterm) Remove(observers []Observer, o Observer) []Observer {
	for j, ob := range observers {
		if ob.GetId() == o.GetId() {
			observers = append(i.Observers[:j], i.Observers[j+1:]...)
			return observers
		}
	}
	return observers
}
